'''
Created on Apr 12, 2011

@author: yc30v1m
'''
from library.AutoLoad import AutoLoad
from logging import getLogger

log = getLogger(__name__)

class DBQueryConnectionMapper(object):
    '''
    classdocs
    '''
    # Ini file reader
    __reader = ''

    # xmlRpcConnection
    __connector = ''
    
    # Connector Type (Valid values are 'soap', 'xmlrpc'
    __connectorType = ''


    
    def __init__(self):
        if self.__reader == '':
            self.__reader = AutoLoad.ClassAsObject('library.Configuration')
            #self.__reader = IniConfReader()
        self.__getConfiguredDbConnector()
        if self.__connectorType == 'soap':
            
            self.__connector = AutoLoad.ClassAsObject('plugins.sudsConnector')
            #self.__connector = sudsConnector()
        elif self.__connectorType == 'xmlrpc':
            self.__connector = AutoLoad.ClassAsObject('plugins.xmlRpcConnector')
        self.__setConnectorUrl()

    def execute(self,dbName, query):
        try:
            self.connect(dbName)
            log.debug("query " + str(query))
            self.__connector.executeQuery(query)
        except:
            log.critical("Query : " + str(query))
            log.critical("DB Name: " + str(dbName))
            AutoLoad.logExceptionInfo(10)
            #raise Exception("error occured")
        
    def rowCount(self):
        return self.__connector.rowCount()
        
    def columnCount(self):
        return self.__connector.columnCount()
        
    def hasNext(self):
        return self.__connector.hasNext()
    
    def getCurrentRow(self):
        return self.__connector.getCurrentRow()

    def getRow(self):
        return self.__connector.getRow()
    
    def getColumnNames(self):
        return self.__connector.getColumnNames()
    
    def getRowValuesAsList(self, row):
        return self.__connector.getRowValuesAsList(row)
    
    def getCurrentRowValuesAsList(self):
        return self.__connector.getCurrentRowValuesAsList()
    
    def getCurrentRowAsDict(self):
        return self.__connector.getCurrentRowAsDict()
    
    def getColumnValueByName(self, colName):
        return self.__connector.getColumnValueByName(colName)
        
    def getColumnValueById(self, id):
        return self.__connector.getColumnValueById(id)
    
    def getNextRow(self):
        return self.__connector.getNextRow()
        
    def moveNext(self):
        self.__connector.moveNext()
            
    def rewind(self):
        self.__connector.rewind()
    
    def getFirstRow(self):
        return self.__connector.getFirstRow()
        
    def getLastRow(self):
        return self.__connector.getLastRow()
    
    def connect(self,dbName):
        try:
            sDriverName = self.__reader.getOptionFromSection(dbName, 'type')
            sDataBaseName = self.__reader.getOptionFromSection(dbName, 'name')
            sHost = self.__reader.getOptionFromSection(dbName, 'host')
            sPort = self.__reader.getOptionFromSection(dbName, 'port')
            sUserName = self.__reader.getOptionFromSection(dbName, 'user')
            sPassword = self.__reader.getOptionFromSection(dbName, 'password')
            #TODO : Add schema to xmlrpcConnector
            sSchema = self.__reader.getOptionFromSection(dbName, 'schema')
            log.debug("DbDetails " + str(dbName))
            self.__connector.connectTo(sDriverName, sDataBaseName, sHost,  sUserName, sPassword, sPort, sSchema)
        except:
            log.critical(sDriverName + " " + sDataBaseName + " " + sHost + " " + sUserName + " " + sPassword + " " + sPort + " " + sSchema)
            AutoLoad.logExceptionInfo(100)
            
    def __setConnectorUrl(self):
        self.__connector.setConnectionUrl(self.__reader.getOptionFromSection('dbconnector', 'dbconnector_url'))
        
    def __getConfiguredDbConnector(self):
        self.__connectorType = self.__reader.getOptionFromSection('dbconnector', 'dbconnector_type')
    